package com.bjsxt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.constants.Constants;
import com.bjsxt.dto.ProducterDto;
import com.bjsxt.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import com.bjsxt.domain.Producter;
import com.bjsxt.mapper.ProducterMapper;
import com.bjsxt.service.ProducterService;
import org.springframework.context.annotation.Bean;

// dubbo中默认重试次数为两次 对于一些非幂等操作(多次调用会有多次不同的结果)我们希望它们重试
// 故而在此处设置重试次数为0
@Service(methods = {@Method(name = "insertProducter",retries = 0)}) //暴露服务
public class ProducterServiceImpl implements ProducterService{

    @Autowired
    private ProducterMapper producterMapper;

    // 分页查询
    @Override
    public DataGridView listProducterPage(ProducterDto producterDto) {
        Page<Producter> page = new Page<>(producterDto.getPageNum(),producterDto.getPageSize());
        // 条件构造器
        QueryWrapper<Producter> qw = new QueryWrapper<>();
        // 根据生产厂家名称模糊查询
        qw.like(StringUtils.isNotBlank(producterDto.getProducterName()),Producter.COL_PRODUCTER_NAME,producterDto.getProducterName());
        // 根据关键字模糊查询
        qw.like(StringUtils.isNotBlank(producterDto.getKeywords()),Producter.COL_KEYWORDS,producterDto.getKeywords());
        // 根据生产厂家电话进行查询
        qw.like(StringUtils.isNotBlank(producterDto.getProducterTel()),Producter.COL_PRODUCTER_TEL,producterDto.getProducterTel());
        // 根据状态进行查询
        qw.eq(StringUtils.isNotBlank(producterDto.getStatus()),Producter.COL_STATUS,producterDto.getStatus());
        // 根据创建时间进行查询
        qw.ge(null!=producterDto.getBeginTime(),Producter.COL_CREATE_TIME,producterDto.getBeginTime());
        qw.le(null!=producterDto.getEndTime(),Producter.COL_CREATE_TIME,producterDto.getEndTime());
        producterMapper.selectPage(page,qw);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    // 根据id查询
    @Override
    public Producter getOne(Long producterId) {
        return producterMapper.selectById(producterId);
    }

    // 添加
    @Override
    public int insertProducter(ProducterDto producterDto) {
//        System.out.println("进入远程调用");
//        int num = 10/0;
        Producter producter = new Producter();
        BeanUtil.copyProperties(producterDto,producter);
        producter.setCreateBy(producterDto.getSimpleUser().getUserName());
        producter.setCreateTime(DateUtil.date());
        return producterMapper.insert(producter);
    }

    // 修改
    @Override
    public int updateProducter(ProducterDto producterDto) {
        Producter producter = new Producter();
        BeanUtil.copyProperties(producterDto,producter);
        producter.setUpdateTime(DateUtil.date());
        return producterMapper.updateById(producter);
    }

    // 删除
    @Override
    public int deleteProducter(Long[] producterIds) {
        if (producterIds.length>0){
            return producterMapper.deleteBatchIds(Arrays.asList(producterIds));
        }
        return 0;
    }

    // 查询有效的
    @Override
    public List<Producter> selectAllProducter() {
        QueryWrapper<Producter> qw = new QueryWrapper<>();
        qw.eq(Producter.COL_STATUS, Constants.STATUS_TRUE);
        return producterMapper.selectList(qw);
    }
}
